package cms.service.access;

import cms.bean.ErrorView;
import cms.bean.setting.SystemSetting;
import cms.bean.user.AccessUser;
import cms.bean.user.User;
import cms.common.data.ApiResult;
import cms.common.data.ApiResultCode;
import cms.common.exception.ServiceException;
import cms.service.access.model.RedEnvelope;
import cms.service.setting.SettingService;
import cms.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 发红包
 */
@Service
public class RedEnvelopeAccessService implements AccessService<RedEnvelope> {
    @Resource
    UserService userService;
    @Resource
    SettingService settingService;

    /**
     * 红包
     *
     * @param accessUser
     * @param act
     * @param request
     * @param redEnvelope
     * @return
     */
    @Override
    public ApiResult access(AccessUser accessUser, String act, HttpServletRequest request, RedEnvelope redEnvelope) {
        if (accessUser == null) {
            throw new ServiceException(ApiResultCode.NO_LOGIN_SUCCESS);
        }
        User user = userService.findUserByUserName(accessUser.getUserName());//查询用户数据
        //红包
        if (redEnvelope.getType() != null && redEnvelope.getType() > 0 && user != null) {
            if (redEnvelope.getGiveQuantity() == null) {
                //error.put("giveQuantity", ErrorView._139.name());//不能为空
                throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._130.name()));
            }
            if (redEnvelope.getGiveQuantity() <= 0) {
                // error.put("giveQuantity", ErrorView._130.name());//请填写正整数
                throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._130.name()));
            }
            if (redEnvelope.getType().equals(20)) {//红包类型 随机金额
                if (redEnvelope.getTotalAmount() != null) {
                    if (redEnvelope.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
//                        error.put("totalAmount", ErrorView._225.name());//不能小于0
                        throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._225.name()));
                    }
                    checkRedTotal(redEnvelope.getTotalAmount(), user);
                } else {
//                    error.put("totalAmount", ErrorView._134.name());//总金额不能为空
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._134.name()));
                }

                //按单个红包金额为0.01计算总金额
                BigDecimal total = new BigDecimal("0.01").multiply(new BigDecimal(redEnvelope.getGiveQuantity()));//最低单个红包金额为0.01元
                if (total.compareTo(redEnvelope.getTotalAmount()) > 0) {
//                    error.put("giveQuantity", ErrorView._135.name());//拆分后最低单个红包金额不足0.01元
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._135.name()));
                }
            } else if (redEnvelope.getType().equals(30)) {//红包类型 固定金额
                if (redEnvelope.getSingleAmount() != null) {
                    if (redEnvelope.getSingleAmount().compareTo(new BigDecimal("0.01")) < 0) {
//                        error.put("singleAmount", ErrorView._137.name());//不能小于0.01元
                        throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._137.name()));
                    }
                } else {
//                    error.put("singleAmount", ErrorView._136.name());//金额不能为空
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._136.name()));
                }
                //计算总金额
                BigDecimal total = redEnvelope.getSingleAmount().multiply(new BigDecimal(redEnvelope.getGiveQuantity()));
                checkRedTotal(total, user);
                redEnvelope.setTotalAmount(total);
            }
        }
        //使用了发红包
        redEnvelope.setHaveRedEnvelope(true);
        return ApiResult.buildSuccess();
    }

    /**
     * 校验总红包金额
     *
     * @param total
     * @param user
     */
    private void checkRedTotal(BigDecimal total, User user) {
        if (total.compareTo(user.getDeposit()) > 0) {
//                        error.put("totalAmount", ErrorView._224.name());//不能大于账户预存款
            throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._224.name()));
        }
        SystemSetting systemSetting = settingService.findSystemSettingCache();
        if (systemSetting.getGiveRedEnvelopeAmountMax() != null) {
            if (systemSetting.getGiveRedEnvelopeAmountMax().compareTo(new BigDecimal("0")) == 0) {
//                            error.put("redEnvelopeLimit", ErrorView._131.name());//不允许发红包
                throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._131.name()));
            } else if (systemSetting.getGiveRedEnvelopeAmountMax().compareTo(new BigDecimal("0")) > 0) {
                if (total.compareTo(systemSetting.getGiveRedEnvelopeAmountMin()) < 0) {
//                                error.put("redEnvelopeLimit", ErrorView._132.name());//不能小于发红包总金额下限
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._132.name()));
                }
                if (total.compareTo(systemSetting.getGiveRedEnvelopeAmountMax()) > 0) {
//                                error.put("redEnvelopeLimit", ErrorView._133.name());//不能大于发红包总金额上限
                    throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._133.name()));
                }
            }
        } else {
            if (total.compareTo(systemSetting.getGiveRedEnvelopeAmountMin()) < 0) {
//                            error.put("redEnvelopeLimit", ErrorView._132.name());//不能小于发红包总金额下限
                throw new ServiceException(ApiResultCode.PARAM_ERROR.setMessage(ErrorView._132.name()));
            }
        }
    }
}
